<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header class="el-header">
      <div>
        <img src="../assets/logo.png" width="35px" height="35px" alt />
        <span>电商平台后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 菜单 -->
      <el-aside class="el-aside" width="200px">
        <el-menu v-if="menus" background-color="#333744" text-color="#fff" active-text-color="#ffd04b" unique-opened
          router :default-active="currentActiveMeunItem">
          <el-sub-menu :index="item.id + ''" v-for="item in menus" :key="item.id">
            <template #title>
              <el-icon :size="20"><Menu/></el-icon>
              <span>{{ item.text }}</span>
            </template>
            <el-menu-item :index="'/' + subItem.url" v-for="subItem in item.children" :key="subItem.id"
              @click="savaCurrentActiveMeunItem('/' + subItem.url)">
              <el-icon :size="20"><CaretRight/></el-icon>
              <span slot="title">{{ subItem.text }}</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
        <ul id="array-rendering"></ul>
      </el-aside>
      <!-- 操作区 -->
      <el-main class="el-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup> 
import { ref, onMounted, getCurrentInstance, nextTick } from "vue";

const { proxy } = getCurrentInstance();
const menus = ref(null);
const currentActiveMeunItem = ref(null);
currentActiveMeunItem.value = window.sessionStorage.getItem(
  "currentActiveMeunItem"
);

// 退出登录
const logout = () => {
  window.sessionStorage.clear();
  proxy.$router.push("/login");
};

// 向服务器发送get请求，获取系统功能菜单
const getMenus = async () => {
  const { data: res } = await proxy.$axios.get("menus");
  menus.value = res.data[0].children; 
};

// 保存当前点击的菜单项
const savaCurrentActiveMeunItem = (activeMeunItem) => {
  window.sessionStorage.setItem("currentActiveMeunItem", activeMeunItem);
  currentActiveMeunItem.value = activeMeunItem;
};

onMounted(() => {
  getMenus();
});



</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  >div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
}

.el-menu {
  border-right: none;
}

.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}

.iconfont {
  margin-right: 10px;
}
</style>
